package cnbot.com.irobot;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.cnbot.irobotvoice.api.IRoboTtsTools;
import com.cnbot.irobotvoice.api.IRobotAsrTools;
import com.cnbot.irobotvoice.api.IRobotNlpTools;
import com.cnbot.irobotvoice.bean.NlpBean;
import com.cnbot.irobotvoice.listener.IRobotAsrResultListener;
import com.cnbot.irobotvoice.listener.IRobotNlpResultListener;
import com.cnbot.irobotvoice.listener.IRobotTtsResultListener;
import com.google.gson.Gson;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private final String TAG = MainActivity.class.getSimpleName();

    /**
     * 语音识别
     */
    private IRobotAsrTools iRobotAsrTools;

    /**
     * 语义分析
     */
    private IRobotNlpTools iRobotNlpTools;

    /**
     * 语音合成
     */
    private IRoboTtsTools iRoboTtsTools;

    private TextView tvAsrResult, tvNlpResult;
    private Button btnStartAsr;

    private ActHandler actHandler;

    private final int HANDLER_ASR_RESULT = 1;
    private final int HANDLER_ASR_STARTUS = 2;
    private class ActHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case HANDLER_ASR_RESULT:
                    String text = (String) msg.obj;
                    tvAsrResult.setText(text);
                    break;
                case HANDLER_ASR_STARTUS:

                    break;
                    default:break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initAsr();
        initNlp();
        initTts();
    }

    private void initView() {
        tvAsrResult = (TextView) findViewById(R.id.tv_asr_result);
        tvNlpResult = (TextView) findViewById(R.id.tv_nlp_result);
        btnStartAsr = (Button) findViewById(R.id.button);

        actHandler = new ActHandler();
    }

    /**
     * 初始化语音识别
     */
    private void initAsr(){
        iRobotAsrTools = IRobotAsrTools.getInstance(true);
        iRobotAsrTools.asrInit();
        iRobotAsrTools.setiRobotAsrResultListener(new IRobotAsrResultCallback());
    }

    /**
     * 初始化语义分析
     */
    private void initNlp(){
        iRobotNlpTools = IRobotNlpTools.getInstance(true);
        iRobotNlpTools.initNlp();
        iRobotNlpTools.setiRobotNlpResultListener(new IRobotNlpResultCallback());
    }

    /**
     * 初始化tts
     */
    private void initTts(){
        iRoboTtsTools = IRoboTtsTools.getInstance(true);
        iRoboTtsTools.initTts();
        iRoboTtsTools.setiRobotTtsResultListener(new IRobotTtsResultCallback());
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                btnStartAsr.setText("开始说话");
                iRobotAsrTools.startAsr();
                break;
            case R.id.button2:
                btnStartAsr.setText("点击说话");
                iRobotAsrTools.stopAsr();
                break;
            default:break;
        }
    }


    public class IRobotAsrResultCallback implements IRobotAsrResultListener{

        @Override
        public void asrResultFinal(final String text) {
            Log.e(TAG, "识别结果 = " + text);
            iRobotNlpTools.startTextNlp(text);
            Message msg = actHandler.obtainMessage();
            msg.what = HANDLER_ASR_RESULT;
            msg.obj = text;
            actHandler.sendMessage(msg);

        }

        @Override
        public void asrError(String error) {
            Log.e(TAG, "识别出错 = ");
        }

        @Override
        public void asrResult(String text) {

        }

        @Override
        public void asrBefore() {
            Log.e(TAG, "开始说话 = ");
            btnStartAsr.setText("正在识别");
        }

        @Override
        public void asrEnd() {
            Log.e(TAG, "正在分析 = ");
            btnStartAsr.setText("正在分析");
        }
    }



    public class IRobotNlpResultCallback implements IRobotNlpResultListener{

        @Override
        public void robotNlpResult(String text) {
            NlpBean nlpBean = new Gson().fromJson(text, NlpBean.class);
            String content = nlpBean.getContent();

            tvNlpResult.setText(content);
            iRoboTtsTools.startTts(content);
        }

        @Override
        public void robotNlpError(String error) {
            tvNlpResult.setText(error);
            btnStartAsr.setText("点击说话");
        }
    }

    public class IRobotTtsResultCallback implements IRobotTtsResultListener{

        @Override
        public void ttsError(String error) {
            btnStartAsr.setText("合成失败");
        }

        @Override
        public void ttsBefore() {
            btnStartAsr.setText("正在播报");
        }

        @Override
        public void ttsEnd() {
            btnStartAsr.setText("点击说话");
        }
    }
    @Override
    protected void onStop() {
        super.onStop();
        iRobotAsrTools.removeiRobotAsrResultListener();
        iRobotNlpTools.removeiRobotNlpResultListener();
    }
}
